package com.wrupple.muba.catalogs.client.module.services.presentation.impl;

import java.util.Collection;
import java.util.Map;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.cellview.client.TextHeader;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.CatalogFieldMap;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.module.services.logic.CatalogDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldConversionStrategyProvider;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.presentation.GenericTableInitializer;
import com.wrupple.muba.catalogs.client.widgets.fields.column.FieldColumn;
import com.wrupple.muba.catalogs.client.widgets.fields.column.ForeignEntryColumn;
import com.wrupple.muba.catalogs.client.widgets.fields.column.ForeignEntryColumn.JsArrayAdapterCell;
import com.wrupple.muba.catalogs.domain.JsCatalogKey;
import com.wrupple.muba.common.client.FieldConversionStrategy;
import com.wrupple.vegetate.domain.CatalogDescriptor;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class GenericTableInitializerImpl implements GenericTableInitializer {
	
	
	private CatalogFieldMap fieldFactory;
	
	private FieldConversionStrategyProvider conversionService;
	
	public static final String GENERIC_KEY_CELL_WIDGETID ="genericKey";
	public static final String GENERIC_VALUE_CELL_WIDGETID = "genericValue";
	
	@Inject	
	public GenericTableInitializerImpl(CatalogDescriptionService descriptionService, CatalogFieldMap fieldFactory,
			FieldDescriptionService descriptor,FieldConversionStrategyProvider conversionService) {
		super();
		this.fieldFactory = fieldFactory;
		this.conversionService=conversionService;
	}


	
	


	@Override
	public  void initialize(CatalogDescriptor catalog,Map<String, FieldDescriptor> summaryFields, CellTable<JsCatalogKey> display) {
		Collection<FieldDescriptor> summaries = summaryFields.values();
		
		Header<String> header;
		Column<JsCatalogKey, ?> column;
		Cell columnCell;
		Cell<String> genericKeyCell;
		FieldConversionStrategy fieldDataConversionStrategy;
		String widget;
		String fieldId;
		String fieldName;
		boolean isKey ;
		boolean ephemeral;
		
		
		for (FieldDescriptor field : summaries) {
			//gather data
			fieldName = field.getName();
			widget = field.getWidget();
			fieldId = field.getId();
			isKey = field.isKey();
			ephemeral = field.isEphemeral();
			
			//create column title
			header = new TextHeader(fieldName);
			//assemble column cell
			columnCell = fieldFactory.get(widget).createCell(null, null, field, Mode.READ);
			//column data type conversion strategy
			fieldDataConversionStrategy = conversionService.getFieldConversionStrategy(field.getId(), catalog);
			
			try {
				if(isKey||ephemeral){
					genericKeyCell=(Cell<String>) fieldFactory.get(GENERIC_KEY_CELL_WIDGETID).createCell(null, null, field, Mode.READ);;
					//weird column
					column = new ForeignEntryColumn(new JsArrayAdapterCell(columnCell, genericKeyCell), field, fieldDataConversionStrategy);
					
				}else{
					// just extract the field value
					column = new FieldColumn(fieldId,columnCell,fieldDataConversionStrategy);
				}
				
			} catch (Exception e) {
				column = null;
				GWT.log("Attempt to create column failed. catalgog:" + catalog.getName() + " field:" + field.getId()
						+ " widget:" + field.getWidget(), e);
			}
			if (column != null) {
				column.setSortable(field.isSortable());
				display.addColumn( column, header);
			}
		}
		
		
	}
	

}
